﻿
using HandyControl.Interactivity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfDemo.Common
{
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();
        public static Control WindFrmName { get; set; }
        public static UserControl UCWindFrmName { get; set; }
        public static void Regiter<T>(string key)
        {
            if (!_RegisterWindow.ContainsKey(key))
                _RegisterWindow.Add(key, new FrmWin() { winType = typeof(T), winds = null });
        }
        public static void Regiter(string key, Type t)
        {
            if (!_RegisterWindow.ContainsKey(key))
                _RegisterWindow.Add(key, new FrmWin() { winType = t, winds = null });
        }
        public static void Remove(string key)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="key"></param>
        /// <param name="successObject">操作成功，关闭窗体返回的值</param>
        public static void CloseFrm(string key, Object successObject)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                try
                {
                    var fw = (FrmWin)_RegisterWindow[key];
                    fw.successObject = successObject;
                    fw.winds.Close();
                }
                catch (Exception)
                {
                    throw new Exception("窗体不存在！");
                }
            }
        }
        /// <summary>
        /// 根据窗体名称，通过反射打开新窗体
        /// </summary>
        /// <param name="key">窗体名称</param>
        /// <param name="obj">参数列表</param>
        public static Object ShowDialog(string key, object[] obj)
        {
            if (!_RegisterWindow.ContainsKey(key))
            {
                throw new Exception("没有注册此键！");
            }        
            var fw = (FrmWin)_RegisterWindow[key];
            var win = (Window)Activator.CreateInstance((Type)fw.winType, obj);
            fw.winds = win;
            fw.successObject = null;          
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ShowLayer();
            
            win.ShowDialog();
            CancleLayer();


            return fw.successObject;
        }
        public static void CreateMask(this Window outputWindow)
        {
            Visual parent = null;
            if (Application.Current.Windows.Count > 0)
                parent = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);
            var _layer = GetAdornerLayer(parent);
            if (_layer == null) return;
            var _adornerContainer = new AdornerContainer(_layer)
            {
                Child = new MaskControl(parent)
            };
            _layer.Add(_adornerContainer);
            if (outputWindow != null)
            {
                outputWindow.Closed += delegate
                {
                    if (parent != null)
                        _layer.Remove(_adornerContainer);
                };
            }
        }
        static AdornerLayer GetAdornerLayer(Visual visual)
        {
            var decorator = visual as AdornerDecorator;
            if (decorator != null)
                return decorator.AdornerLayer;
            var presenter = visual as ScrollContentPresenter;
            if (presenter != null)
                return presenter.AdornerLayer;
            var visualContent = (visual as Window)?.Content as Visual;
            return AdornerLayer.GetAdornerLayer(visualContent ?? visual);
        }
        /// <summary>
        /// 显示遮罩层
        /// </summary>
        /// <param name="owner"></param>
        public static void ShowLayer()
        {
            try
            {
                Window owner = new Window();
                if (Application.Current.Windows.Count > 0)
                    owner = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);

                //蒙板
                Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
                //父级窗体原来的内容
                UIElement original = owner.Content as UIElement;
                owner.Content = null;
                //容器Grid
                Grid container = new Grid();
                container.Children.Add(original);//放入原来的内容
                container.Children.Add(layer);//在上面放一层蒙板
                                              //将装有原来内容和蒙板的容器赋给父级窗体
                owner.Content = container;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 关闭遮罩层
        /// </summary>
        /// <param name="owner"></param>
        public static void CancleLayer()
        {
            Window owner = new Window();
            if (Application.Current.Windows.Count > 0)
                owner = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);
            Grid grid = null;
            //容器Grid
            grid = owner.Content as Grid;
            if (grid == null) return;
            //父级窗体原来的内容
            UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
            //将父级窗体原来的内容在容器Grid中移除
            grid.Children.Remove(original);
            //赋给父级窗体
            owner.Content = original;
        }
    }

    public class FrmWin
    {
        public string key { get; set; }
        public Type winType { get; set; }
        public Window winds { get; set; }

        /// <summary>
        /// 操作成功后返回的对象
        /// </summary>
        public Object successObject { get; set; }
    }
}
